package com.sdkj.service.goods.applygoods;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.sdkj.dao.DaoSupport;
import com.sdkj.entity.Page;
import com.sdkj.entity.User;
import com.sdkj.util.Const;
import com.sdkj.util.PageData;
import com.sdkj.util.Tools;
import com.sdkj.util.UuidUtil;

@Service("applygoodsService")
public class ApplyGoodsService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/*
	 * 新增
	 */
	public void save(PageData pd) throws Exception {
		dao.save("ApplyGoodsMapper.save", pd);
		
		String str = pd.getString("apply_kind_num");// 已选择的物品
		String idsStr = pd.getString("goods_id");// 已选择的物品id
		String[] strs = str.split(",");
		String[] ids = idsStr.split(",");
		// 打印纸-单价20.5-数量2-共41元
		for (int i = 0; i < strs.length; i++) {
			String[] params = strs[i].split("-");
			String name = params[0];
			String price = params[1].substring(params[1].lastIndexOf("价") + 1);
			String num = params[2].substring(params[2].lastIndexOf("量") + 1);
			String account = params[3].substring(
					params[3].lastIndexOf("共") + 1, params[3].lastIndexOf("元"));
			PageData pdDetail = new PageData();
			pdDetail.put("apply_goods_detail_id", UuidUtil.get32UUID());
			pdDetail.put("apply_goods_id", pd.getString("apply_goods_id"));
			pdDetail.put("goods_id", ids[i]);
			pdDetail.put("goods_name", name);
			pdDetail.put("goods_price", price);
			pdDetail.put("goods_num", num);
			pdDetail.put("goods_account", account);
			pdDetail.put("apply_remark", "");
			dao.save("ApplyGoodsDetailMapper.save", pdDetail);

		}

	}

	/*
	 * 删除
	 */
	public void delete(PageData pd) throws Exception {
		dao.delete("ApplyGoodsMapper.delete", pd);
	}

	/*
	 * 修改
	 */
	public void edit(PageData pd) throws Exception {
		dao.update("ApplyGoodsMapper.edit", pd);
		
	}
	/*
	 * 入库修改
	 */
	public void editPutIn(PageData pd) throws Exception {
		PageData pdApply = new PageData();
		pdApply.put("apply_acount_price",pd.getString("apply_acount_price"));
		pdApply.put("apply_goods_id",pd.getString("apply_goods_id"));
		pdApply.put("putin_state", 2);
		dao.update("ApplyGoodsMapper.edit", pdApply);
		User user=(User)pd.get("user");
		String[] ids = pd.getArray("apply_goods_detail_id");
		System.out.println("ids.length="+ids.length);
		for (int i = 0; i < ids.length; i++) {
			// 生成记录
			PageData pdPutin = new PageData();
			String goods_id = pd.getString("goods_id" + ids[i]);
			String put_num = pd.getString("put_num" + ids[i]);
			pdPutin.put("putin_id", UuidUtil.get32UUID());
			pdPutin.put("create_time", Tools.date2Str(new Date())); // 创建时间
			pdPutin.put("buy_id", ids[i]); // 申请详情主键
			pdPutin.put("goods_id", goods_id);// 物品类型主键
			pdPutin.put("put_num", put_num);// 入库数量
			pdPutin.put("user_name",user.getUSERNAME());
			pdPutin.put("user_section", "暂无部门");
			dao.save("PutinWarehouseMapper.save",pdPutin);
			// 增加库存
			PageData pdWR = new PageData();
			pdWR.put("goods_id", goods_id);
			pdWR.put("goods_num", put_num);
			pdWR.put("use_num", null);
			pdWR.put("last_time", Tools.date2Str(new Date()));
			dao.update("WarehouseRecordMapper.edit",pdWR);
		}

		
		
		
		
		
	}
	/*
	 * 列表
	 */
	public List<PageData> list(Page page) throws Exception {
		return (List<PageData>) dao.findForList(
				"ApplyGoodsMapper.datalistPage", page);
	}

	/*
	 * 列表(全部)
	 */
	public List<PageData> listAll(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("ApplyGoodsMapper.listAll", pd);
	}

	/*
	 * 通过id获取数据
	 */
	public PageData findById(PageData pd) throws Exception {
		return (PageData) dao.findForObject("ApplyGoodsMapper.findById", pd);
	}

	/*
	 * 批量删除
	 */
	public void deleteAll(String[] ArrayDATA_IDS) throws Exception {
		dao.delete("ApplyGoodsMapper.deleteAll", ArrayDATA_IDS);
	}

}
